﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ColdSun.Commons;
using ColdSun.Commons.Configs;
using ColdSun.Commons.Entities;
using ColdSun.Services;
using System.IO;
using ColdSun.Framework;

namespace ColdSun.BankManagements
{
    public partial class CtrlInsurance : UserControl
    {
        private FrmProgressBar frmProgressBar = new FrmProgressBar();
        private Insurance selectInsurance = new Insurance();
        private string infoFile;
        public CtrlInsurance()
        {
            InitializeComponent();
        }

        private void btnInsuranceExport_Click(object sender, EventArgs e)
        {
            BackgroundWorker backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += new DoWorkEventHandler(BackgroundInsuranceWorker_DoWork);
            backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundInsuranceWorker_RunWorkerCompleted);

            // Kick off the Async thread
            backgroundWorker.RunWorkerAsync();

            // Lock up the UI with this modal progress form.
            frmProgressBar.ShowDialog();
        }

        private void BackgroundInsuranceWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Logger.Info(string.Format("----- BEGIN TO PROCESS INSURANCE (WEEK) -----"));
            try
            {
                List<Replacement> replacementInfos = Utilities.GetReplacementInfo(this.infoFile);
                if (replacementInfos == null)
                {
                    Logger.Info(string.Format("Can not get replacement information"));
                    return;
                }

                AccountService accountService = AccountService.GetInstance(txtFollowingFile.Text, dtpExportFromDate.Value, dtpExportToDate.Value);
                Logger.Info(string.Format("Process for {0}", this.selectInsurance.Name));

                List<Account> sets = accountService.GetSettlementAccount;
                InsuranceService service = new InsuranceService();
                string exportFile = txtExportFile.Text;
                string result = service.GenerateInsuranceByBranchForWeek(
                    txtOriginalFile.Text, replacementInfos[0], accountService,
                    this.selectInsurance, dtpExportDate.Value, dtpExportFromDate.Value,
                    dtpExportToDate.Value, ref exportFile, txtExportFolder.Text);

                service.GenerateSettlementSheet(exportFile, txtFollowingFile.Text, accountService, this.selectInsurance);
            }
            catch (Exception ex)
            {
                Logger.Error("Background worker - Do work", ex);
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
            finally
            {
                Logger.Info(string.Format("----- END TO PROCESS INSURANCE (WEEK) -----"));
            }
        }

        private void BackgroundInsuranceWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (frmProgressBar != null)
            {
                frmProgressBar.Close();
            }
        }

        

        private void CtrlInsurance_Load(object sender, EventArgs e)
        {
            //Insurance
            if (File.Exists(Utilities.GetFullFileName(CommonConfigs.InsuranceOriginalFile)))
            {
                txtOriginalFile.Text = Utilities.GetFullFileName(CommonConfigs.InsuranceOriginalFile);
            }
            if (File.Exists(Utilities.GetFullFileName(CommonConfigs.DefaultFollowingFile)))
            {
                txtFollowingFile.Text = Utilities.GetFullFileName(CommonConfigs.DefaultFollowingFile);
            }
            txtExportFile.Text = CommonConfigs.InsuranceFireExportFile;
            if (Directory.Exists(Utilities.GetFullFileName(CommonConfigs.InsuranceExportFolder)))
            {
                txtExportFolder.Text = Utilities.GetFullFileName(CommonConfigs.InsuranceExportFolder);
            }
            dtpExportDate.Format = DateTimePickerFormat.Custom;
            dtpExportDate.CustomFormat = "dd/MM/yyyy";
            dtpExportFromDate.Format = DateTimePickerFormat.Custom;
            dtpExportFromDate.CustomFormat = "dd/MM/yyyy";
            dtpExportFromDate.Value = Utilities.GetFirstDayOfWeek(dtpExportFromDate.Value.AddDays(-7));
            dtpExportToDate.Format = DateTimePickerFormat.Custom;
            dtpExportToDate.CustomFormat = "dd/MM/yyyy";
            dtpExportToDate.Value = Utilities.GetLastDayOfWeek(dtpExportToDate.Value.AddDays(-7));
            cmbInsuranceType.SelectedIndex = 0;

            this.infoFile = Utilities.GetFullFileName(CommonConfigs.InsuranceFireInfoFile);
        }

        private void cmbInsuranceType_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectInsurance.Type = cmbInsuranceType.SelectedIndex;
            selectInsurance.Name = cmbInsuranceType.SelectedItem.ToString();

            if (selectInsurance.Type <= 1)
            {
                infoFile = Utilities.GetFullFileName(CommonConfigs.InsuranceFireInfoFile);
                txtExportFile.Text = CommonConfigs.InsuranceFireExportFile;
            }
            else
            {
                infoFile = Utilities.GetFullFileName(CommonConfigs.InsuranceIncidentInfoFile);
                txtExportFile.Text = CommonConfigs.InsuranceIncidentExportFile;
            }
        }

        private void btnOriginalFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Excel Files (.xlsx)|*.xlsx|Excel Files (.xls)|*.xls|All Files (*.*)|*.*";
            openFileDialog.FilterIndex = 2;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtOriginalFile.Text = openFileDialog.FileName;
            }
        }

        private void btnFollowingFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Excel Files (.xlsx)|*.xlsx|Excel Files (.xls)|*.xls|All Files (*.*)|*.*";
            openFileDialog.FilterIndex = 1;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtFollowingFile.Text = openFileDialog.FileName;
            }
        }

        private void btnExportFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                txtExportFolder.Text = folderBrowserDialog.SelectedPath;
            }
        }
    }
}
